﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace CodeArt.SharePoint.CamlQuery
{
    public class ItemWrapper
    {
        private SPListItem item;
        public ItemWrapper(SPListItem item)
        {
            this.item = item;
        }

        public string this[string name]
        {
            get
            {
                return ""+this.item[name];
            }
            set
            {
                this.item[name] = value;
            }
        }

        public string GetString(string name)
        {
            return "" + this.item[name];
        }
        
        public int GetInt32(string name)
        {
            var obj = this.item[name];
            if (obj == null)
                return default(int);
            else
                return Convert.ToInt32(obj);
        }

        public SPUser GetUser(string name)
        {
            var value = this[name];
            if(value=="")
                return null;

            SPField field = this.item.Fields.GetField(name);
            SPFieldUserValue userValue = field.GetFieldValue(value) as SPFieldUserValue;
            return userValue.User;
        }

        public string GetUserLoginName(string name)
        {
            var user = this.GetUser(name);
            if (user != null)
                return user.LoginName;
            else
                return string.Empty;
        }
    
    }
}
